
//
//  HJDDetailTimeTableViewController.m
//  JYHospital
//
//  Created by 胡金东 on 16/5/24.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDDetailTimeTableViewController.h"
#import "HJDCTimeModel.h"

@interface HJDDetailTimeTableViewController ()<UIScrollViewDelegate>
{
    int Days;
    int shi;
}

//整体页面
@property (nonatomic,strong)UIScrollView *scroll;

@property (nonatomic,strong)  UILabel *CTimeLbl;

@property (nonatomic,strong)  UILabel *projectLbl;

@property (nonatomic,strong)  UILabel * projectOneLbl;


@property (nonatomic,strong) NSString *strDate;

@property (nonatomic,strong) UILabel  *dateLbl;




@end

@implementation HJDDetailTimeTableViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"产检时间表";
//    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(shareBtnClick) image:@"矢量智能对象" HighImage:nil];
    
    [self setUpWeek];
    [self setUpStess];
    [self setUpProject];
    [self setUpAttention];
}

-(void)setUpWeek
{
    self.scroll = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.scroll.delegate = self;
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.scroll.showsVerticalScrollIndicator = YES;//垂直移动
    self.scroll.showsHorizontalScrollIndicator = NO;//水平移动
    //上下滑动
    self.scroll.contentSize = CGSizeMake(HJDWidth,855 ) ;
   [self.view addSubview: self.scroll];
    
    CGFloat weekX = HJDHorizontalMargin/2;
    CGFloat weekY = HJDVerticalMargin ;
    UILabel *weekLbl = [[UILabel alloc]init];
    weekLbl.font = [UIFont systemFontOfSize:20];
    weekLbl.text = [NSString stringWithFormat:@"孕%@周",self.ctimeModel.weeks];
       CGSize weekSize = [weekLbl.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:weekLbl.font,NSFontAttributeName, nil]];
    weekLbl.frame = (CGRect){{weekX,weekY},weekSize};
    [self.scroll addSubview:weekLbl];
    
    CGFloat CTimeX = HJDHorizontalMargin/2;
    CGFloat CTimeY = CGRectGetMaxY(weekLbl.frame) + 5;
    self.CTimeLbl = [[UILabel alloc]init];
    self.CTimeLbl.font = [UIFont systemFontOfSize:18];
    self.CTimeLbl.text = @"产检时间" ;
    self.CTimeLbl.textColor = [UIColor lightGrayColor];
    CGSize CTimeSize = [self.CTimeLbl.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.CTimeLbl.font,NSFontAttributeName, nil]];
    self.CTimeLbl.frame = (CGRect){{CTimeX,CTimeY},CTimeSize};
    [self.scroll addSubview:self.CTimeLbl];


    CGFloat dateX = CGRectGetMaxX(self.CTimeLbl.frame) + HJDHorizontalMargin * 2;
    CGFloat dateY = CTimeY;
    self.dateLbl = [[UILabel alloc]init];
    self.dateLbl.font = [UIFont systemFontOfSize:18];
    
    
    
    
    
    NSData *sysData = [[NSUserDefaults standardUserDefaults]valueForKey:@"sysPregnancy"];
    NSString *sysStr = [NSKeyedUnarchiver unarchiveObjectWithData:sysData];
    if ([sysStr isKindOfClass:[NSNull class]]){
        NSUserDefaults *defaD = [NSUserDefaults standardUserDefaults];
        NSString *preD = [defaD objectForKey:@"pregnanc"];
        NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
        [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
        [inputFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *inputDate = [inputFormatter dateFromString:preD];
        
        NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:inputDate];
        Days = ((int)time)/(3600*24 *7);
        
        shi = [self.ctimeModel.weeks intValue];
        NSDate *nextDate = [NSDate dateWithTimeInterval:24*60*60 *7*shi sinceDate:inputDate];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *strDate = [dateFormatter stringFromDate:nextDate];
        self.dateLbl.text = strDate;
        
    }else{
        NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
        [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
        [inputFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *inputDate = [inputFormatter dateFromString:sysStr];
        
        NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:inputDate];
        Days = ((int)time)/(3600*24 *7);
        shi = [self.ctimeModel.weeks intValue];
        NSDate *nextDate = [NSDate dateWithTimeInterval:24*60*60 *7*shi sinceDate:inputDate];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
       self.strDate = [dateFormatter stringFromDate:nextDate];
        self.dateLbl.text = self.strDate;
        
    }
    self.dateLbl.textColor = [UIColor lightGrayColor];
    CGSize dateSize = [_dateLbl.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.dateLbl.font,NSFontAttributeName, nil]];
    self.dateLbl.frame = (CGRect){{dateX,dateY},dateSize};
    [self.scroll addSubview:self.dateLbl];
    
    CGFloat weekkX = CGRectGetMaxX(self.dateLbl.frame) + 5;
    CGFloat weekkY = CTimeY;
    UILabel  *weekkLbl = [[UILabel alloc]init];
    weekkLbl.font = [UIFont systemFontOfSize:18];
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"（星期天）", @"（星期一）", @"（星期二）", @"（星期三）", @"（星期四）", @"（星期五）", @"（星期六）", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置 日期 格式 可以根据自己的需求 随时调整， 否则计算的结果可能为 nil
    formatter.dateFormat = @"yyyy-MM-dd";
    // 将字符串日期 转换为 NSDate 类型
    NSDate *endDate = [formatter dateFromString:self.dateLbl.text];
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:endDate];

    weekkLbl.text = [weekdays objectAtIndex:theComponents.weekday];
    
    weekkLbl.textColor = [UIColor lightGrayColor];
    CGSize weekkSize = [weekkLbl.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:weekkLbl.font,NSFontAttributeName, nil]];
    weekkLbl.frame = (CGRect){{weekkX,weekkY},weekkSize};
    [self.scroll addSubview:weekkLbl];
}

-(void)setUpStess
{
    CGFloat stessX = HJDHorizontalMargin/2;
    CGFloat stessY = CGRectGetMaxY(self.CTimeLbl.frame) + HJDVerticalMargin;
    UILabel *stessLbl = [[UILabel alloc]init];
    stessLbl.font = [UIFont systemFontOfSize:20];
    stessLbl.text = @"本周重点";
    CGSize stessSize = [stessLbl.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:stessLbl.font,NSFontAttributeName, nil]];
    stessLbl.frame = (CGRect){{stessX,stessY},stessSize};
    [self.scroll addSubview:stessLbl];
    
    CGFloat projectX = HJDHorizontalMargin/2;
    CGFloat projectY = CGRectGetMaxY(stessLbl.frame) +5;
    self.projectLbl = [[UILabel alloc]init];
   self.projectLbl.font = [UIFont systemFontOfSize:18];
    self.projectLbl.text = self.ctimeModel.emphasis;
    self.projectLbl.textColor = [UIColor lightGrayColor];
    CGSize projectSize = [_projectLbl.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.projectLbl.font,NSFontAttributeName, nil]];
    self.projectLbl.frame = (CGRect){{projectX,projectY},projectSize};
    [self.scroll addSubview:self.projectLbl];

}

-(void)setUpProject
{
    CGFloat cProjectX = HJDHorizontalMargin/2;
    CGFloat cProjectY = CGRectGetMaxY(self.projectLbl.frame) + HJDVerticalMargin;
    UILabel *cProjectLbl = [[UILabel alloc]init];
    cProjectLbl.font = [UIFont systemFontOfSize:20];
    cProjectLbl.text = @"产检项目";
    CGSize cProjectSize = [cProjectLbl.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:cProjectLbl.font,NSFontAttributeName, nil]];
    cProjectLbl.frame = (CGRect){{cProjectX,cProjectY},cProjectSize};
    [self.scroll addSubview:cProjectLbl];
    
    CGFloat projectOneX =HJDHorizontalMargin;
    CGFloat projectOneY = CGRectGetMaxY(cProjectLbl.frame) + 5;
    CGFloat projectOneW = HJDWidth - (HJDHorizontalMargin + 10)* 2;
   self.projectOneLbl = [[UILabel alloc]init];
    self.projectOneLbl.numberOfLines = 0;
    NSString *str = [self.ctimeModel.items stringByReplacingOccurrencesOfString:@";" withString:@"\n\n"];
    self.projectOneLbl.text = str;
    self.projectOneLbl.textColor = [UIColor lightGrayColor];
    CGRect projectOneRect = [self.projectOneLbl.text boundingRectWithSize:CGSizeMake(projectOneW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:self.projectOneLbl.font,NSFontAttributeName, nil] context:nil];
    self.projectOneLbl.frame = (CGRect){{projectOneX,projectOneY},projectOneRect.size};
    [self.scroll addSubview:self.projectOneLbl];
    
}

-(void)setUpAttention
{
    CGFloat weekCareX = HJDHorizontalMargin/2;
    CGFloat weekCareY = CGRectGetMaxY(self.projectOneLbl.frame) + HJDVerticalMargin;
    UILabel *weekCareLbl = [[UILabel alloc]init];
    weekCareLbl.font = [UIFont systemFontOfSize:20];
    weekCareLbl.text = @"本周注意";
    CGSize weekCareSize = [weekCareLbl.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:weekCareLbl.font,NSFontAttributeName, nil]];
    weekCareLbl.frame = (CGRect){{weekCareX,weekCareY},weekCareSize};
    [self.scroll addSubview:weekCareLbl];
    
    CGFloat careContentX = HJDHorizontalMargin;
    CGFloat careContentY = CGRectGetMaxY(weekCareLbl.frame) + 5;
    CGFloat careContentW = HJDWidth - HJDHorizontalMargin ;
    UILabel *careContentLbl = [[UILabel alloc]init];
    careContentLbl.numberOfLines = 0;
    careContentLbl.text = self.ctimeModel.notice;
    careContentLbl.textColor = [UIColor lightGrayColor];
    CGRect careContentRect = [careContentLbl.text boundingRectWithSize:CGSizeMake(careContentW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:self.projectOneLbl.font,NSFontAttributeName, nil] context:nil];
    careContentLbl.frame = (CGRect){{careContentX,careContentY},careContentRect.size};
    [self.scroll addSubview:careContentLbl];
    
}

//-(void)shareBtnClick
//{
//    NSLog(@"分享");
//}

@end
